const  local = {
    // 存储数据
    setItem(key: string, value: any) {
      try {
        if (value === null) {
          window.localStorage.setItem(key, 'null');
        } else if (typeof value === "object") {
          window.localStorage.setItem(key, JSON.stringify(value));
        } else {
          window.localStorage.setItem(key, value);
        }
      } catch (error) {
        console.error("Error setting item in localStorage:", error);
      }
    },
  
    // 获取数据
    getItem(key: string): any {
      const value = window.localStorage.getItem(key);
      if (value === null) {
        return null;
      }
  
      try {
        // 尝试解析 JSON 字符串
        return JSON.parse(value);
      } catch (error) {
        // 如果解析失败，返回原始字符串
        return value;
      }
    },
    removeItem(key: string): void {
      window.localStorage.removeItem(key);
    },
  
    // 获取时间问候语
    getHours(): string {
      const hours = new Date().getHours();
      switch (true) {
        case hours >= 6 && hours <= 10:
          return "早上好";
        case hours > 10 && hours <= 14:
          return "中午好";
        case hours > 14 && hours <= 18:
          return "下午好";
        default:
          return "晚上好";
      }
    },
  };

  export default local